auto();
requestScreenCapture(false); //竖屏截图
sleep(1000);


// app应用关闭
function closeApp(packageName) {
  app.openAppSetting(packageName);
  sleep(random(1000, 2000));
  text(app.getAppName(packageName)).waitFor();
  if (text("强行停止").exists()) {
    let b = text("强行停止").findOne();
    click(b.bounds().centerX(), b.bounds().centerY()); sleep(2000);
    if(text("强行停止").exists()) {
      let b = text("强行停止").findOne();
      click(b.bounds().centerX(), b.bounds().centerY()); sleep(2000);
    }
    log(app.getAppName(packageName) + "应用已被关闭");
    back();
    sleep(random(8000, 10000));
  } else {
    log(app.getAppName(packageName) + "应用不能被正常关闭或不在后台运行");
    back();
    sleep(random(1000, 2000));
  }
}

// 查询虚拟机状态再关闭
// closeApp('com.vmos.alhs');


function startVMAutoJs() {
  if (className("android.view.View").bounds(0, 270, 1080, 2070).exists()) {
    click(540, 2000); sleep(2000);  //点击home
    click(540, 1440); sleep(8000);   //点击虚拟机中的 audojs
    click(867, 980); sleep(5000);  // 点击运行虚拟机脚本
    click(540, 900); sleep(3000);   // 无障碍选择页面点击autojs6
    var img = captureScreen();
    var c = images.pixel(img, 990, 545);
    if (colors.toString(c) != '#6B777D') {
      click(950, 545); sleep(2000);
      click(900, 1270); sleep(2000);
    }
    click(950, 545); sleep(2000);
    click(900, 1590);
  }
}

function vmstart(i) {
  launch("com.vmos.alhs");
  sleep(5000);
  xstep = (i % 3) * 329;
  ystep = parseInt(i / 3) * 732;
  click(100 + xstep, 570 + ystep); sleep(1000);   // 已经开启虚拟机 (185,774,237,812)
  if (className("android.widget.TextView").boundsInside(185 + xstep, 774 + ystep, 237 + xstep, 812 + ystep).text("进入").exists()) {
    click(210 + xstep, 725 + ystep); sleep(1000);// 点击进入
  } else {
    click(300 + xstep, 785 + ystep); sleep(2000);// 点击开机
    while (true) { if (text("正在开机中").exists()) { sleep(1000) } else { break }; } sleep(2000);
    className("android.view.View").bounds(0, 270, 1080, 2070).waitFor();
  }
  startVMAutoJs();
}

function vmshutdown(i) {
  launch("com.vmos.alhs");
  sleep(5000);
  xstep = (i % 3) * 329;
  ystep = parseInt(i / 3) * 732;
  click(100 + xstep, 570 + ystep); sleep(1000);   
  if (className("android.widget.TextView").boundsInside(185 + xstep, 774 + ystep, 237 + xstep, 812 + ystep).text("进入").exists()) { // 已经开启虚拟机 (185,774,237,812)
    click(210 + xstep, 390 + ystep); sleep(1000);// 点击关机
  }
}


/** 0 关机  0-1已开机 0-2 已打开无障碍 
 * 1- 运行任务中 1-0 打开高德   1-1:7000 骑行中  1-2:4000 步行中   
 * 2 任务结束 2-0 任务完成已关机 */
var getStatus = function (phone) {
  if (phone) {
    try {
      http.__okhttp__.setTimeout(1000);
      var res = http.get('http://54.169.75.19:8001/status/' + phone + '/');
      if (res.statusCode != 200) {
        log('服务器异常')
        return '';
      }
      return res.body.string();
    } catch (error) {
      log('服务器异常')
    }
  }
  return '';
}



function getVMs() {
  waitForPackage('com.vmos.alhs');
  let vms={};
  for (let i = 0; i < 9; i++) {
    let x = 64; let w = 314 - 62; // 66 左右加宽2像素
    let y = 243; let h = 300 - 20;
    xstep = (i % 3) * 329;
    ystep = parseInt(i / 3) * 725;
    // log(x + xstep, y + ystep, x + xstep + w, y + ystep + h);
    if (className('android.widget.TextView').boundsInside(x + xstep, y + ystep, x + xstep + w, y + ystep + h).exists()) {
      let phone = className('android.widget.TextView').boundsInside(x + xstep, y + ystep, x + xstep + w, y + ystep + h).findOne().text().substring(0, 4);
      // log('phone: ' + phone);
      vms[i]=phone;
    }
  }
  return vms;
}

function getRunningVM(vms){
  let running = [];
  for (let i = 0; i < 9; i++) {
    phone = vms[i];
    if (phone){
      let status = getStatus(phone);
      if (!status || status == '' || status == '0') {
        log(phone + '未开机')
      } else if (status == '2') {
        log(phone + '已完成')
      } else {
        log(phone + '运行中')
        running.push(phone)
      }
    }
  }
  return running;
}

function getFinishVM(vms){
  let finish = {};
  for (let i = 0; i < 9; i++) {
    phone = vms[i];
    if (phone){
      let status = getStatus(phone);
      if (status && status == '2') {
        finish[i]=phone;
      }
    }
  }
  return finish;
}


launch('com.vmos.alhs'); sleep(5000);
vmstart(3);
exit();
vms = getVMs();
run_vm = getRunningVM(vms)
if (run_vm.length == 0) {
  log('没有进行中的虚拟机')
  // closeApp("com.vmos.alhs");
}

// launch('com.vmos.alhs'); sleep(10000);
finish_vm = [];
while(true){
  launch('com.vmos.alhs'); sleep(5000); waitForPackage('com.vmos.alhs');
  finish_vm = getFinishVM(vms);
  for (var key in finish_vm){
    vmshutdown(key+1);
  }
  // if(finish_vm.length==6){
  //   break;
  // }
  run_vm = getRunningVM(vms);
  if (run_vm.length==2){
    sleep(5*60*1000); // 5分钟
    continue;
  }
  for(var key in vms){
    if(finish_vm[key] || run_vm.indexOf(vms[key])>=0){
      continue;
    }
    log('开机'+ (parseInt(key)+1))
    vmstart(parseInt(key));
    sleep(2*60*1000);
    break;
  }
}

//

